package com.ce.pms.system.custservice.service.impl;


import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.IFrequentEventDao;
import com.ce.pms.system.custservice.model.FrequentEventDO;
import com.ce.pms.system.custservice.service.IFrequentEventService;
import com.ce.pms.system.custservice.vo.FrequentEventVO;
import com.sitechasia.webx.core.support.Page;


/**
 * 描述：频发事件service实现
 * @author 王浩羽
 *
 */
public class FrequentEventServiceImpl extends AbstractCustomerEventServiceImpl<FrequentEventVO, FrequentEventDO>
	implements IFrequentEventService
{
	private IFrequentEventDao frequentEventDao;


	public boolean addFrequentEvent(FrequentEventVO frequentEventVO)
	{
		if(!BlankUtil.isBlank(frequentEventVO) && !BlankUtil.isBlank(frequentEventVO.getName()))
		{
			FrequentEventDO frequentEventDO = assembleV2D(frequentEventVO, null);
			frequentEventDO.setSysInfo(createInfo(frequentEventVO.getCreator()));
			return frequentEventDao.saveObject(frequentEventDO) != null;
		}
		return false;
	}


	public boolean deleteFrequentEventById(Long frequentEventId)
	{
		if(frequentEventId == null)
		{
			return false;
		}
		try
		{
			frequentEventDao.deleteObjectById(frequentEventId);
			return true;
		}
		catch(RuntimeException e)
		{ // 如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("custservice:system_delete_inused_exception", BusinessException.WARNING);
		}
	}


	public boolean editFrequentEvent(FrequentEventVO frequentEventVO)
	{
		if(!BlankUtil.isBlank(frequentEventVO) && !BlankUtil.isBlank(frequentEventVO.getKeyword()))
		{
			FrequentEventDO frequentEventDO = frequentEventDao.findById(frequentEventVO.getKeyword());
			if(frequentEventDO == null)
			{
				return false;
			}
			frequentEventDO.setName(frequentEventVO.getName());
			frequentEventDO.setRemark(frequentEventVO.getRemark());
			frequentEventDO.setSequence(frequentEventVO.getSequence());
			frequentEventDO.setValidity(frequentEventVO.getValidity());
			modifyInfo(frequentEventDO.getSysInfo(), frequentEventVO.getModifier());
			return frequentEventDao.updateObject(frequentEventDO) != null;
		}
		return false;
	}


	public List<FrequentEventVO> queryAllFrequentEvent()
	{
		List<FrequentEventDO> frequentEventDOList = frequentEventDao.findAll();
		 sortList(frequentEventDOList);
		return assembleDOList2VOList(frequentEventDOList);
	}
   /**
    * 功能描述:查询所有有效的频发事件<br>
    * @return
    * Create author:yixianjian <br>
    * Create on:2009-1-16  <br>
    */
	 @SuppressWarnings("unchecked")
	public List<FrequentEventVO> queryAllFrequentEventWithValidity(){
		 List<FrequentEventDO> frequentEventDOList = frequentEventDao.crtieriaFindBy("validity", "1").list();
		 sortList(frequentEventDOList);
		 return assembleDOList2VOList(frequentEventDOList);
	 }

	public Page queryFrequentEventListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}


	public void setFrequentEventDao(IFrequentEventDao frequentEventDao)
	{
		this.frequentEventDao = frequentEventDao;
	}


	@Override
	protected FrequentEventDO findObjectDOById(Long id)
	{
		return frequentEventDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = frequentEventDao.queryByPage(searchVO);
		List<FrequentEventDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}

	@Override
	protected int compareDO(FrequentEventDO o1, FrequentEventDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}
	
	@Override
	protected boolean isNameExisit(String name)
	{
		// 名称可以重复
		return false;
	}


	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "custservice:system_name_exist_exception";
	}
}
